package com.desmart.renjiao.main.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.desmart.renjiao.R;
import com.desmart.renjiao.contants.Constants;
import com.desmart.renjiao.http.ApiUrl;
import com.desmart.renjiao.main.bean.BannerView;
import com.desmart.renjiao.main.bean.HomeIcons;
import com.desmart.renjiao.main.bean.Title;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.common.router.Contanst;
import com.lemon.library.common.router.JumpUtils;
import com.lemon.library.utils.DensityUtils;
import com.lemon.library.utils.FullyGridLayoutManager;
import com.lemon.library.utils.ToastUtils;
import com.lemon.library.view.slidepager.BannerBean;
import com.lemon.library.view.slidepager.BannerPager;

import java.util.ArrayList;
import java.util.List;

public class HomeTypeAdapter extends BaseMultiItemQuickAdapter<HomeItemType,BaseViewHolder> {

    private int count;


    public HomeTypeAdapter(List<HomeItemType> data, int count) {
        super(data);
        this.count = count;
        addItemType(HomeItemType.TITLE, R.layout.main_home_type_title);
        addItemType(HomeItemType.ICON, R.layout.main_home_type_icon);
        addItemType(HomeItemType.EMPTY, R.layout.main_home_type_empty);
        addItemType(HomeItemType.Banner, R.layout.main_home_type_banner);
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeItemType item) {
        switch (helper.getItemViewType()) {
            case HomeItemType.TITLE:
                bindTitle(helper, item);
                break;
            case HomeItemType.ICON:
                bindIcon(helper,item);
                break;
            case HomeItemType.Banner:
                bindBanner(helper,item);
                break;
        }
    }

    private void bindBanner(BaseViewHolder helper, HomeItemType item) {
        BannerPager banner = helper.itemView.findViewById(R.id.banner);
        BannerView bean = (BannerView) item;
        ArrayList<BannerBean> alist = bean.getAlist();
        if (alist != null && alist.size() > 0) {
            banner.setEasyData(alist);
            banner.setAuto(true);
            banner.setDuring(5000);
            banner.setLooper(true);
            banner.setIndicatorPosition(BannerPager.GRAVITY_CENTER, DensityUtils.dip2px(8), 30, 20);
            banner.setImageClickListener(new BannerPager.ImageClickLintener() {
                @Override
                public void click(int position) {
                    String title = alist.get(position).getTitle();
                    String url = alist.get(position).getUrl();
                    JumpUtils.toWeb(mContext,url,title);
                }
            });
            banner.setVisibility(View.VISIBLE);
        }
    }

    private void bindIcon(BaseViewHolder helper, HomeItemType item) {
        RecyclerView recyclerview = helper.itemView.findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new FullyGridLayoutManager(mContext,count));
        HomeIcons icons = (HomeIcons) item;
        List<HomeIcons.Icon> list = icons.getList();
        HomeIconAdapter iconAdapter = new HomeIconAdapter(list);
        iconAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                int title = list.get(position).getTitle();
                onIconClick(title);
            }
        });
        recyclerview.setAdapter(iconAdapter);
    }

    private void onIconClick(int title) {
        switch (title){
            case R.string.yuangongzizhu:
                ARouter.getInstance().build(ARoutePath.workplace.EmployeeSelfServiceActivity).navigation(mContext);
                break;
            case R.string.tushuguan:
                JumpUtils.toWeb(mContext,Constants.LIBRARY,mContext.getString(R.string.tushuguan));
                break;
            case R.string.jiaocaiyijianfankui:
                JumpUtils.toWeb(mContext,Constants.JIAOCAI,mContext.getString(R.string.jiaocaiyijianfankui));
                break;
            case R.string.bianjishijie:
                JumpUtils.toWeb(mContext,Constants.Edit_World,mContext.getString(R.string.bianjishijie));
                break;
            case R.string.bendanweigonggao:
                ARouter.getInstance().build(ARoutePath.home.AnnouncementListActivity)
                        .withString(Contanst.annnouncement.Key_Type,Contanst.annnouncement.type_ben).navigation(mContext);
                break;
            case R.string.jituangonggao:
                ARouter.getInstance().build(ARoutePath.home.AnnouncementListActivity)
                        .withString(Contanst.annnouncement.Key_Type,Contanst.annnouncement.type_Ji).navigation(mContext);
                break;
            case R.string.the_news:
                ARouter.getInstance().build(ARoutePath.home.AnnouncementListActivity)
                        .withString(Contanst.annnouncement.Key_Type,Contanst.annnouncement.the_news).navigation(mContext);
                break;
            case R.string.kaoqin:
                ARouter.getInstance().build(ARoutePath.workplace.AttendanceRecordActivity).navigation();
                break;
            case R.string.danfeichaxun:
                if (onItemClick != null)
                    onItemClick.onItemClick(R.string.danfeichaxun);
                break;
            case R.string.buchongyiliaochaxun:
            case R.string.gerengongzichaxun:
            case R.string.cankaxinxichaxun:
                if (onItemClick != null)
                    onItemClick.onItemClick(title);
                break;
            case R.string.youjian:
                JumpUtils.toWeb(mContext,ApiUrl.openEmail,"IBM iNotes | 收件箱");
                break;
            case R.string.daiban:
                ARouter.getInstance().build(ARoutePath.home.CommissionActivity).navigation();
                break;
            default:
                ToastUtils.showGravity("暂未开发");
                break;
        }
    }

    private void bindTitle(BaseViewHolder helper, HomeItemType item) {
        TextView tvTitle = helper.itemView.findViewById(R.id.tv_title);
        Title title = (Title) item;
        tvTitle.setText(title.getName());
    }

    public OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface OnItemClick{

        void onItemClick(int title);

    }

}
